/*
 * @Author: Salaing
 * @Date: 2024-06-11 22:05:36
 * @LastEditors: Salaing
 */
import $http from 'api';
export default {
  namespaces: 'common',
  state: {
    collapse: false,
    isShowDetailDialog: false,
    isClearForm: false,
    ids: [],
  },
  subscriptions: {
    setup({ dispatch, history }) {
      // 初始化查询用户是否登录 app.start阶段进行
      dispatch({
        type: 'queryUserLogin',
        payload: { history },
      });
    },

  },
  reducers: {
    changeCollapse: (state, { payload }) => {
      return {
        ...state,
        ...payload,
      };
      // return { collapse: payload.collapse };
    },
    setShowDetailDialog: (state, { payload }) => {
      return { ...state, ...payload };
    },
    // 清空搜索表单
    clearForm: (state, { payload }) => {
      return { ...state, ...payload };
    },
    //- 保存需要删除的id集合
    saveSelectIds: (state, { payload }) => ({ ...state, ...payload }),
  },
  effects: {
    * queryUserLogin({ payload }, { put, call }) {
      const {
        history,
        history: {
          location: { pathname },
        },
      } = payload;
      if (pathname !== '/users/login' && pathname !== '/users/forgetPassword') {
        if (
          !sessionStorage.getItem('userProfile') ||
          !sessionStorage.getItem('token') ||
          !sessionStorage.getItem('routeList')
        ) {
          history.replace('/users/login');
        } else {
          // 满足条件，检测用户信息
          const res = yield call($http.queryUserLogin);
          if (res.code !== 0) return;
          const { data: routeList } = yield call($http.getRoutesList);
          sessionStorage.setItem('routeList', JSON.stringify(routeList || []));
        }
      } else {
        // 不需要拦截
        sessionStorage.clear();
      }
      // 判断当前用户的访问路径
    },
  },
};
